package com.example.student.web.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @author TonySong
 * @date 2025/8/15 0015
 * @time 17:08
 */
@WebFilter("/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取请求的uri
        String uri = request.getRequestURI();
        //获取session
        HttpSession session= request.getSession();
        if(session!=null || uri.contains("login")){
            filterChain.doFilter(request,response);
        }else{
            response.sendRedirect(request.getContextPath()+"/user?action=toLogin");
        }

    }
}
